package com.hjy.scheduled.task.job;

import com.hjy.scheduled.config.service.RedisReadWriteService;
import com.hjy.scheduled.mysql.repository.quartzRepository;
import com.hjy.scheduled.util.CronUtil;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import com.hjy.scheduled.mysql.entity.quartz;

import java.util.Date;

public class taskJob implements Job {

    @Autowired
    private RedisReadWriteService redis;
    @Autowired
    private quartzRepository quartz;

    @Override
    public void execute(JobExecutionContext context){
        try {

            // 获取 JobDataMap , 并从中取出参数
            JobDataMap data = context.getJobDetail().getJobDataMap();
            String id = data.getString("id");
            String name = data.getString("name");
            String cron = data.getString("cron");

            System.out.println("id为："+id+"，名字为："+name+"在时间为："+cron+"的表达式时间上执行任务。。。。。。。。。。");
            //如果执行成功，将该ID的数据的type更新为执行完成！

            //下一个
            quartz q = new quartz();
            q.setName(name);
            q.setType(Short.parseShort("0"));
            Date oldTime = CronUtil.getCronToDate(cron);
            Date newTime = new Date(oldTime.getTime() + 30*1000);
            q.setCron(CronUtil.getCron(newTime));
            quartz.save(q);

        } catch (Exception e) {
            //如果执行错误，也需要更新type
            e.printStackTrace();
        }

    }
}
